package pl.squeaky.mvc.nature.wizard;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;

import pl.squeaky.mvc.dialog.FolderSelectionDialog;

public class MvcPathsPage extends WizardPage {
	private IProject project;
	private Text txtController;
	private Text txtModel;
	private Text txtView;
	private Button btnController;
	private Button btnModel;
	private Button btnView;

	protected MvcPathsPage(IProject project) {
		super("MvcPathsPage");
		
		this.project = project;
		
		setTitle("Add MVC Support");
		setDescription("Configure MVC paths.");
	}
	
	class BrowseListener implements SelectionListener {
		@Override
		public void widgetDefaultSelected(SelectionEvent e) {
		}

		@Override
		public void widgetSelected(SelectionEvent e) {
			//TODO: remove
			System.out.println("selected!");
			
			Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
			
			FolderSelectionDialog dialog = new FolderSelectionDialog(shell, project);
			if(dialog.open() == Dialog.OK) {
				IFolder folder = (IFolder)dialog.getFirstResult();
				//TODO: remove
				System.out.println(folder.getProjectRelativePath().toString());
				String path = folder.getProjectRelativePath().toString();
				
				if(e.widget == btnController) {
					txtController.setText(path);
				} else if(e.widget == btnModel) {
					txtModel.setText(path);
				} else if(e.widget == btnView) {
					txtView.setText(path);
				}
			}
		}
		
	}
	
	@Override
	public void createControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout(3, false);
		composite.setLayout(layout);
		setControl(composite);
		
		BrowseListener listener = new BrowseListener();
		
		GridData textData = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
		
		Label lblController = new Label(composite, SWT.NONE);
		lblController.setText("Controller path:");
		
		txtController = new Text(composite, SWT.BORDER | SWT.SINGLE);
		txtController.setLayoutData(textData);
		
		btnController = new Button(composite, SWT.BORDER);
		btnController.setText("B&rowse...");
		btnController.addSelectionListener(listener);
		
		Label lblModel = new Label(composite, SWT.NONE);
		lblModel.setText("Model path:");
		
		txtModel = new Text(composite, SWT.BORDER | SWT.SINGLE);
		txtModel.setLayoutData(textData);
		
		btnModel = new Button(composite, SWT.BORDER);
		btnModel.setText("B&rowse...");
		btnModel.addSelectionListener(listener);
		
		Label lblView = new Label(composite, SWT.NONE);
		lblView.setText("View path:");
		
		txtView = new Text(composite, SWT.BORDER | SWT.SINGLE);
		txtView.setLayoutData(textData);
		
		btnView = new Button(composite, SWT.BORDER);
		btnView.setText("B&rowse...");
		btnView.addSelectionListener(listener);
	}
	
	public String getControllerPath() {
		return txtController.getText();
	}
	
	public String getModelPath() {
		return txtModel.getText();
	}
	
	public String getViewPath() {
		return txtView.getText();
	}

}
